package com.net.xpay.admin.controller.admin.rbac;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.common.util.StreamUtil;
import com.net.xpay.admin.po.RoleMenuPo;
import com.net.xpay.admin.secutiry.RequestUser;
import com.net.xpay.common.domain.admin.rbac.AdminUser;
import com.net.xpay.common.domain.admin.rbac.Menu;
import com.net.xpay.common.domain.admin.rbac.RoleMenu;
import com.net.xpay.common.manager.admin.rbac.MenuManager;
import com.net.xpay.common.manager.admin.rbac.RoleMenuManager;
import com.net.xpay.common.model.admin.rbac.MenuModel;
import com.net.xpay.common.service.admin.rbac.MenuService;
import com.net.xpay.common.service.admin.rbac.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 2019/6/4 15:21

 */
@RestController
@RequestMapping("/admin/menu")
@Api(tags = "运营/菜单管理", description = "运营/菜单管理 net")
public class AdminMenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuManager menuManager;

    @Autowired
    private RoleMenuManager roleMenuManager;

    @Autowired
    private RoleService roleService;

    @ApiOperation("构建菜单")
    @GetMapping("/build")
    @ResponseBody
    public ResultList<MenuModel> buildMenu(@RequestUser AdminUser adminUser) {
        List<Menu> menuList = menuService.buildMenus(adminUser.getId(), adminUser.getSuperAdmin());
        List<MenuModel> menuVos = menuService.buildMenuModels(menuList);
        return new ResultList<>(menuVos);
    }

    @ApiOperation("获取所有的菜单树")
    @GetMapping("/tree")
    @ResponseBody
    public ResultList<Map<String, Object>> getMenusTree(@RequestUser AdminUser adminUser) {
        List<Menu> menus = menuManager.findByPid(0L);
        menus.sort(Comparator.comparing(Menu::getSort));
        List<Map<String, Object>> menuTree = menuService.getMenuTree(menus);
        return new ResultList<>(menuTree);
    }

    @ApiOperation("获取某个角色的菜单id集合")
    @GetMapping("/id")
    @ResponseBody
    public ResultList<Long> buildMenu(@RequestUser AdminUser adminUser, Long roleId) {
        List<RoleMenu> roleMenus = roleMenuManager.listByRoleId(roleId);
        return new ResultList<>(StreamUtil.map(roleMenus, RoleMenu::getMenuId));
    }

    @ApiOperation("分配角色菜单")
    @PostMapping("/save-role-menu")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'menu::save-role-menu')")
    public ResultData<Boolean> saveRoleMenu(@RequestUser AdminUser adminUser, @RequestBody RoleMenuPo po) {
        Preconditions.checkArgument(po != null && po.getRoleId() != null);
        roleService.saveRoleMenu(po.getRoleId(), po.getMenuIds());
        return new ResultData<>(true);
    }

    @GetMapping("/list-menu-tree")
    @ApiOperation("获取菜单列表,整理为树形")
    @PreAuthorize("hasAnyRole('ADMIN', 'menu::list-menu')")
    public ResultData<List<Menu>> listMenuTree() {
        List<Menu> menuListModel = menuService.listMenu();
        return new ResultData<>(menuListModel);
    }

    @DeleteMapping("/delete-menu/{id}")
    @ApiOperation("删除菜单(目录,按钮)")
    @PreAuthorize("hasAnyRole('ADMIN', 'menu::delete-menu')")
    public ResultData<Boolean> deleteMenu(@PathVariable Long id) {
        boolean delete = menuService.deleteMenu(id);
        if (delete) {
            return new ResultData<>(true);
        }
        return new ResultData<>(false);
    }

    @GetMapping("/list-no-button-menu-tree")
    @ApiOperation("获取菜单列表数据(不包含按钮)")
    public ResultData<List<Menu>> listNoButtonMenuTree() {
        List<Menu> menus = menuService.listNoButtonMenuTree();
        return new ResultData<>(menus);
    }

    @GetMapping("/get-menu-by-id/{id}")
    @ApiOperation("根据id获取menu数据")
    public ResultData<Menu> getMenuById(@PathVariable Long id) {
        Menu menu = menuManager.getById(id);
        // 设置上级名称
        Menu byId = menuManager.getById(menu.getPid());
        if (byId == null) {
            menu.setParentName("一级菜单");
        } else {
            menu.setParentName(byId.getName());
        }
        return new ResultData<>(menu);
    }

    @PostMapping("/update-menu")
    @ApiOperation("修改菜单")
    @PreAuthorize("hasAnyRole('ADMIN', 'menu::update-menu')")
    public ResultData<Boolean> updateMenu(@RequestBody Menu menu) {
        Boolean update = menuService.saveOrUpdateMenu(menu);
        if (update) {
            return new ResultData<>(true);
        }
        return new ResultData<>(false);
    }

    @PostMapping("/add-menu")
    @ApiOperation("新增菜单")
    @PreAuthorize("hasAnyRole('ADMIN', 'menu::add-menu')")
    public ResultData<Boolean> addMenu(@RequestBody Menu menu) {
        Boolean saveOrUpdateMenu = menuService.saveOrUpdateMenu(menu);
        if (saveOrUpdateMenu) {
            return new ResultData<>(true);
        }
        return new ResultData<>(false);
    }

}
